
package com.fengmoxi.ctv.manage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;

import com.fengmoxi.ctv.model.AppModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class AppDataManage {

    private final Context mContext;
    private final List<String> mBlockedApps = new ArrayList<>();

    public AppDataManage(Context context) {
        mContext = context;
        mBlockedApps.add(mContext.getApplicationContext().getPackageName());
        mBlockedApps.add("com.android.launcher3");
        mBlockedApps.add("com.google.android.tvlauncher");
        mBlockedApps.add("com.android.settings");
        mBlockedApps.add("com.android.tv.settings");
        mBlockedApps.add("com.google.android.tv.remote.service");
        mBlockedApps.add("com.android.traceur");
        mBlockedApps.add("com.rockchips.mediacenter");
        mBlockedApps.add("com.android.calculator2");
        mBlockedApps.add("com.android.camera2");
    }

    public ArrayList<AppModel> getLaunchAppList() {
        PackageManager pm = mContext.getPackageManager();
        final Intent intent1 = new Intent();
        final Intent intent2 = new Intent();
        intent1.setAction(Intent.ACTION_MAIN);
        intent2.setAction(Intent.ACTION_MAIN);
        intent1.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
        intent2.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> localList = pm.queryIntentActivities(intent1, 0);
        localList.addAll(pm.queryIntentActivities(intent2, 0));

        ArrayList<String> packageNames = new ArrayList<>();
        ArrayList<AppModel> localArrayList;
        Iterator<ResolveInfo> localIterator = null;
        localArrayList = new ArrayList<>();
        if (localList.size() != 0) {
            localIterator = localList.iterator();
        }
        while (true) {
            assert localIterator != null;
            if (!localIterator.hasNext()) break;
            ResolveInfo localResolveInfo = (ResolveInfo) localIterator.next();
            AppModel localAppBean = new AppModel();
            localAppBean.setIcon(localResolveInfo.activityInfo.loadIcon(pm));
            localAppBean.setName(localResolveInfo.activityInfo.loadLabel(pm).toString());
            localAppBean.setPackageName(localResolveInfo.activityInfo.packageName);
            localAppBean.setDataDir(localResolveInfo.activityInfo.applicationInfo.publicSourceDir);
            localAppBean.setLauncherName(localResolveInfo.activityInfo.name);
            String pkgName = localResolveInfo.activityInfo.packageName;
            PackageInfo mPackageInfo;
            try {
                mPackageInfo = mContext.getPackageManager().getPackageInfo(pkgName, 0);
                if ((mPackageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {// 系统预装
                    localAppBean.setSysApp(true);
                }
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
            // 查重
            if (packageNames.contains(localAppBean.getPackageName())) {
                continue;
            }
            // 黑名单检测
            if (mBlockedApps.contains(localAppBean.getPackageName())) {
                continue;
            }
            packageNames.add(localAppBean.getPackageName());
            localArrayList.add(localAppBean);
        }
        return localArrayList;
    }

    public ArrayList<AppModel> getUninstallAppList() {
        PackageManager localPackageManager = mContext.getPackageManager();
        Intent localIntent = new Intent("android.intent.action.MAIN");
        localIntent.addCategory("android.intent.category.LAUNCHER");
        @SuppressLint("QueryPermissionsNeeded") List<ResolveInfo> localList = localPackageManager.queryIntentActivities(localIntent, 0);
        ArrayList<AppModel> localArrayList = null;
        Iterator<ResolveInfo> localIterator = null;
        localArrayList = new ArrayList<>();
        localIterator = localList.iterator();
        while (localIterator.hasNext()) {
            ResolveInfo localResolveInfo = (ResolveInfo) localIterator.next();
            AppModel localAppBean = new AppModel();
            localAppBean.setIcon(localResolveInfo.activityInfo.loadIcon(localPackageManager));
            localAppBean.setName(localResolveInfo.activityInfo.loadLabel(localPackageManager).toString());
            localAppBean.setPackageName(localResolveInfo.activityInfo.packageName);
            localAppBean.setDataDir(localResolveInfo.activityInfo.applicationInfo.publicSourceDir);
            String pkgName = localResolveInfo.activityInfo.packageName;
            PackageInfo mPackageInfo;
            try {
                mPackageInfo = mContext.getPackageManager().getPackageInfo(pkgName, 0);
                if ((mPackageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {// 系统预装
                    localAppBean.setSysApp(true);
                } else {
                    localArrayList.add(localAppBean);
                }
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return localArrayList;
    }

    public ArrayList<AppModel> getAutoRunAppList() {
        PackageManager localPackageManager = mContext.getPackageManager();
        Intent localIntent = new Intent("android.intent.action.MAIN");
        localIntent.addCategory("android.intent.category.LAUNCHER");
        @SuppressLint("QueryPermissionsNeeded") List<ResolveInfo> localList = localPackageManager.queryIntentActivities(localIntent, 0);
        ArrayList<AppModel> localArrayList;
        Iterator<ResolveInfo> localIterator;
        localArrayList = new ArrayList<>();
        localIterator = localList.iterator();

        while (localIterator.hasNext()) {
            ResolveInfo localResolveInfo = localIterator.next();
            AppModel localAppBean = new AppModel();
            localAppBean.setIcon(localResolveInfo.activityInfo.loadIcon(localPackageManager));
            localAppBean.setName(localResolveInfo.activityInfo.loadLabel(localPackageManager).toString());
            localAppBean.setPackageName(localResolveInfo.activityInfo.packageName);
            localAppBean.setDataDir(localResolveInfo.activityInfo.applicationInfo.publicSourceDir);
            String pkgName = localResolveInfo.activityInfo.packageName;
            String permission = "android.permission.RECEIVE_BOOT_COMPLETED";
            try {
                PackageInfo mPackageInfo = mContext.getPackageManager().getPackageInfo(pkgName, 0);
                if ((PackageManager.PERMISSION_GRANTED == localPackageManager.checkPermission(permission, pkgName))
                        && !((mPackageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0)) {
                    localArrayList.add(localAppBean);
                }
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return localArrayList;
    }
}
